package com.hice.demo.sbdbra.common.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtils
{
  public static final String EXT = ".zip";
  private static final String BASE_DIR = "";
  private static final String PATH = "/";
  private static final int BUFFER = 1024;

  public static void compress(File srcFile)
    throws Exception
  {
    String name = srcFile.getName();
    String basePath = srcFile.getParent();
    String destPath = basePath + name + ".zip";
    compress(srcFile, destPath);
  }

  public static void compress(File srcFile, File destFile)
    throws Exception
  {
    CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(destFile), new CRC32());

    ZipOutputStream zos = new ZipOutputStream(cos);

    compress(srcFile, zos, "");

    zos.flush();
    zos.close();
  }

  public static void compress(File srcFile, String destPath)
    throws Exception
  {
    compress(srcFile, new File(destPath));
  }

  private static void compress(File srcFile, ZipOutputStream zos, String basePath)
    throws Exception
  {
    if (srcFile.isDirectory())
      compressDir(srcFile, zos, basePath);
    else
      compressFile(srcFile, zos, basePath);
  }

  public static void compress(String srcPath)
    throws Exception
  {
    File srcFile = new File(srcPath);
    compress(srcFile);
  }

  public static void compress(String srcPath, String destPath)
    throws Exception
  {
    File srcFile = new File(srcPath);
    compress(srcFile, destPath);
  }

  private static void compressDir(File dir, ZipOutputStream zos, String basePath)
    throws Exception
  {
    File[] files = dir.listFiles();
    ZipEntry entry;
    if (files.length < 1) {
      entry = new ZipEntry(basePath + dir.getName() + "/");

      zos.putNextEntry(entry);
      zos.closeEntry();
    }

    for (File file : files)
    {
      compress(file, zos, basePath + dir.getName() + "/");
    }
  }

  private static void compressFile(File file, ZipOutputStream zos, String dir)
    throws Exception
  {
    ZipEntry entry = new ZipEntry(dir + file.getName());

    zos.putNextEntry(entry);

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

    byte[] data = new byte[1024];
    int count;
    while ((count = bis.read(data, 0, 1024)) != -1) {
      zos.write(data, 0, count);
    }
    bis.close();

    zos.closeEntry();
  }
}